home *** CD-ROM | disk | FTP | other *** search
/ Introduction to 3D Game …ogramming with DirectX 12 / Introduction-to-3D-Game-Programming-with-DirectX-12.ISO / Code.Textures / Chapter 9 Texturing / TexColumns / Shaders / Default.hlsl next >
Encoding:
Text File  |  2016-03-02  |  3.7 KB  |  136 lines

  1. //***************************************************************************************
  2. // Default.hlsl by Frank Luna (C) 2015 All Rights Reserved.
  3. //***************************************************************************************
  4.  
  5. // Defaults for number of lights.
  6. #ifndef NUM_DIR_LIGHTS
  7.     #define NUM_DIR_LIGHTS 3
  8. #endif
  9.  
  10. #ifndef NUM_POINT_LIGHTS
  11.     #define NUM_POINT_LIGHTS 0
  12. #endif
  13.  
  14. #ifndef NUM_SPOT_LIGHTS
  15.     #define NUM_SPOT_LIGHTS 0
  16. #endif
  17.  
  18. // Include structures and functions for lighting.
  19. #include "LightingUtil.hlsl"
  20.  
  21. Texture2D    gDiffuseMap : register(t0);
  22.  
  23.  
  24. SamplerState gsamPointWrap        : register(s0);
  25. SamplerState gsamPointClamp       : register(s1);
  26. SamplerState gsamLinearWrap       : register(s2);
  27. SamplerState gsamLinearClamp      : register(s3);
  28. SamplerState gsamAnisotropicWrap  : register(s4);
  29. SamplerState gsamAnisotropicClamp : register(s5);
  30.  
  31. // Constant data that varies per frame.
  32. cbuffer cbPerObject : register(b0)
  33. {
  34.     float4x4 gWorld;
  35.     float4x4 gTexTransform;
  36. };
  37.  
  38. // Constant data that varies per material.
  39. cbuffer cbPass : register(b1)
  40. {
  41.     float4x4 gView;
  42.     float4x4 gInvView;
  43.     float4x4 gProj;
  44.     float4x4 gInvProj;
  45.     float4x4 gViewProj;
  46.     float4x4 gInvViewProj;
  47.     float3 gEyePosW;
  48.     float cbPerObjectPad1;
  49.     float2 gRenderTargetSize;
  50.     float2 gInvRenderTargetSize;
  51.     float gNearZ;
  52.     float gFarZ;
  53.     float gTotalTime;
  54.     float gDeltaTime;
  55.     float4 gAmbientLight;
  56.  
  57.     // Indices [0, NUM_DIR_LIGHTS) are directional lights;
  58.     // indices [NUM_DIR_LIGHTS, NUM_DIR_LIGHTS+NUM_POINT_LIGHTS) are point lights;
  59.     // indices [NUM_DIR_LIGHTS+NUM_POINT_LIGHTS, NUM_DIR_LIGHTS+NUM_POINT_LIGHT+NUM_SPOT_LIGHTS)
  60.     // are spot lights for a maximum of MaxLights per object.
  61.     Light gLights[MaxLights];
  62. };
  63.  
  64. cbuffer cbMaterial : register(b2)
  65. {
  66.     float4   gDiffuseAlbedo;
  67.     float3   gFresnelR0;
  68.     float    gRoughness;
  69.     float4x4 gMatTransform;
  70. };
  71.  
  72. struct VertexIn
  73. {
  74.     float3 PosL    : POSITION;
  75.     float3 NormalL : NORMAL;
  76.     float2 TexC    : TEXCOORD;
  77. };
  78.  
  79. struct VertexOut
  80. {
  81.     float4 PosH    : SV_POSITION;
  82.     float3 PosW    : POSITION;
  83.     float3 NormalW : NORMAL;
  84.     float2 TexC    : TEXCOORD;
  85. };
  86.  
  87. VertexOut VS(VertexIn vin)
  88. {
  89.     VertexOut vout = (VertexOut)0.0f;
  90.     
  91.     // Transform to world space.
  92.     float4 posW = mul(float4(vin.PosL, 1.0f), gWorld);
  93.     vout.PosW = posW.xyz;
  94.  
  95.     // Assumes nonuniform scaling; otherwise, need to use inverse-transpose of world matrix.
  96.     vout.NormalW = mul(vin.NormalL, (float3x3)gWorld);
  97.  
  98.     // Transform to homogeneous clip space.
  99.     vout.PosH = mul(posW, gViewProj);
  100.     
  101.     // Output vertex attributes for interpolation across triangle.
  102.     float4 texC = mul(float4(vin.TexC, 0.0f, 1.0f), gTexTransform);
  103.     vout.TexC = mul(texC, gMatTransform).xy;
  104.     
  105.     return vout;
  106. }
  107.  
  108. float4 PS(VertexOut pin) : SV_Target
  109. {
  110.     float4 diffuseAlbedo = gDiffuseMap.Sample(gsamAnisotropicWrap, pin.TexC) * gDiffuseAlbedo;
  111.     
  112.     // Interpolating normal can unnormalize it, so renormalize it.
  113.     pin.NormalW = normalize(pin.NormalW);
  114.  
  115.     // Vector from point being lit to eye. 
  116.     float3 toEyeW = normalize(gEyePosW - pin.PosW);
  117.  
  118.     // Light terms.
  119.     float4 ambient = gAmbientLight*diffuseAlbedo;
  120.  
  121.     const float shininess = 1.0f - gRoughness;
  122.     Material mat = { diffuseAlbedo, gFresnelR0, shininess };
  123.     float3 shadowFactor = 1.0f;
  124.     float4 directLight = ComputeLighting(gLights, mat, pin.PosW,
  125.         pin.NormalW, toEyeW, shadowFactor);
  126.  
  127.     float4 litColor = ambient + directLight;
  128.  
  129.     // Common convention to take alpha from diffuse albedo.
  130.     litColor.a = diffuseAlbedo.a;
  131.  
  132.     return litColor;
  133. }
  134.  
  135.  
  136.